﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HR.ProjectX.BLL.Services.VehicleService;
using HR.ProjectX.BLL.Messaging;
using HR.ProjectX.GUI.ViewModels.Vehicle;
using HR.ProjectX.BLL.Services.ManufacturerService;
using HR.ProjectX.BLL.Services.VehicleCategoryService;

namespace HR.ProjectX.GUI.Controllers
{
    public class VehicleController : Controller
    {
        private readonly IVehicleService _vehicleService;
        private readonly IManufacturerService _manufacturerService;
        private readonly IVehicleCategoryService _vehicleCategoryService;

        public VehicleController(IVehicleService vehicleService,
                                 IManufacturerService manufacturerService,
                                 IVehicleCategoryService vehicleCategoryService)
        {
            _vehicleService = vehicleService;
            _manufacturerService = manufacturerService;
            _vehicleCategoryService = vehicleCategoryService;
        }

        public ActionResult Index(string id)
        {
            if (id == null)
            {
                GetAllVehiclesResponse response = _vehicleService.GetAllVehicles();

                VehicleIndexView indexView = new VehicleIndexView();
                indexView.Vehicles = response.Vehicles;
                indexView.VehicleCategory = "All Cars";

                return View(indexView);
            }
            else
            {
                GetAllVehiclesByRequest request = new GetAllVehiclesByRequest();
                request.VehicleCategory = id;

                GetAllVehiclesByResponse response = _vehicleService.GetAllVehiclesByCategory(request);

                VehicleIndexView indexView = new VehicleIndexView();
                indexView.Vehicles = response.Vehicles;
                indexView.VehicleCategory = id;

                return View(indexView);
            }
        }

        public ActionResult Detail(string id)
        {
            GetVehicleByRequest request = new GetVehicleByRequest();
            request.Model = id;

            GetVehicleByResponse response = _vehicleService.GetVehicleByModel(request);

            VehicleDetailView detailView = new VehicleDetailView();
            detailView.Vehicle = response.Vehicle;

            return View(detailView);
        }

        public ActionResult Create()
        {
            VehicleCreateView createView = new VehicleCreateView();

            createView.Manufacturers = _manufacturerService.GetAllManufacturers().Manufacturers;
            createView.VehicleCategories = _vehicleCategoryService.GetAllVehicleCategories().VehicleCategories;

            return View(createView);
        }

        [HttpPost]
        public ActionResult Create(VehicleCreateView createView)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    CreateVehicleRequest request = new CreateVehicleRequest();

                    createView.Manufacturers = _manufacturerService.GetAllManufacturers().Manufacturers;
                    createView.VehicleCategories = _vehicleCategoryService.GetAllVehicleCategories().VehicleCategories;

                    request.Manufacturer = createView.SelectedManufacturer;
                    request.VehicleCategory = createView.SelectedCategory;
                    request.Model = createView.Model;
                    request.RegistrationNumber = createView.RegistrationNumber;
                    request.CurrentKilometers = createView.CurrentKilometers;
                    request.DailyHireRate = createView.DailyHireRate;

                    _vehicleService.CreateVehicle(request);

                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    VehicleCreateView errorView = new VehicleCreateView();

                    errorView.Manufacturers = _manufacturerService.GetAllManufacturers().Manufacturers;
                    errorView.VehicleCategories = _vehicleCategoryService.GetAllVehicleCategories().VehicleCategories;

                    errorView.HasIssue = true;
                    errorView.Message = ex.Message;

                    return View(errorView);
                }
            }
            else
            {
                createView.Manufacturers = _manufacturerService.GetAllManufacturers().Manufacturers;
                createView.VehicleCategories = _vehicleCategoryService.GetAllVehicleCategories().VehicleCategories;
            }

            return View(createView);
        }

    }
}
